﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class BillingExecDA : IBillingExecDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string EXECUTE_DATE = "@EXECUTE_DATE";
        private string SQL_CHECK_BILLING_EXEC_ID_UNIQUE = " SELECT COUNT(1) FROM @_@BILLING_EXEC WHERE ROWID = @ROWID ";
        private string SQL_DELETE_BILLING_EXEC = " DELETE FROM @_@BILLING_EXEC WHERE ROWID = @ROWID ";
        private string SQL_INSERT_BILLING_EXEC = " INSERT INTO @_@BILLING_EXEC ( WH_ID, EXECUTE_DATE, STATUS, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @EXECUTE_DATE, @STATUS, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_BILLING_EXEC = " SELECT  ROWID, WH_ID, EXECUTE_DATE, STATUS, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@BILLING_EXEC ";
        private string SQL_UPDATE_BILLING_EXEC = " UPDATE @_@BILLING_EXEC SET WH_ID = @WH_ID, EXECUTE_DATE = @EXECUTE_DATE, STATUS = @STATUS, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE ROWID = @ROWID  ";
        private string STATUS = "@STATUS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckBillingExecIDUnique(string billingExecID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ROWID", billingExecID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_BILLING_EXEC_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckBillingExecIDUnique(DataBase dataBase, DbTransaction tran, string billingExecID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ROWID", billingExecID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_BILLING_EXEC_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteBillingExec(List<string> billingExecID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int j = 0; j < billingExecID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ROWID", billingExecID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_BILLING_EXEC.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteBillingExec(DataBase dataBase, DbTransaction tran, List<string> billingExecID, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int j = 0; j < billingExecID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ROWID", billingExecID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_BILLING_EXEC.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllBillingExec(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_BILLING_EXEC.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllBillingExec(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_BILLING_EXEC.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public BillingExecInfo GetBillingExecByID(string billingexecID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_BILLING_EXEC.Replace("@_@", whLoginID) + " WHERE ROWID = @ROWID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ROWID", billingexecID) };
            BillingExecInfo billingExecInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    DateTime CS00003;
                    billingExecInfo = new BillingExecInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), (reader["EXECUTE_DATE"].ToString() != "") ? DateTime.Parse(reader["EXECUTE_DATE"].ToString()) : (CS00003 = new DateTime()), reader["STATUS"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00003 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return billingExecInfo;
        }

        public BillingExecInfo GetBillingExecByID(DataBase dataBase, DbTransaction tran, string billingexecID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_BILLING_EXEC.Replace("@_@", whLoginID) + " WHERE ROWID = @ROWID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ROWID", billingexecID) };
            BillingExecInfo billingExecInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                DateTime CS00003;
                billingExecInfo = new BillingExecInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), (reader["EXECUTE_DATE"].ToString() != "") ? DateTime.Parse(reader["EXECUTE_DATE"].ToString()) : (CS00003 = new DateTime()), reader["STATUS"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00003 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return billingExecInfo;
        }

        public DataSet GetBillingExecByQueryList(List<string> sqlWhere, BillingExecQueryEntity billingexecQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_BILLING_EXEC.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!billingexecQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, billingexecQuery.CurrentPage, billingexecQuery.PageSize, billingexecQuery.SortField, billingexecQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetBillingExecByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, BillingExecQueryEntity billingexecQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_BILLING_EXEC.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, billingexecQuery.CurrentPage, billingexecQuery.PageSize, billingexecQuery.SortField, billingexecQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertBillingExec(BillingExecInfo billingExecInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                billingExecInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_BillingExec_Parameters(billingExecInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_BILLING_EXEC.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertBillingExec(DataBase dataBase, DbTransaction tran, BillingExecInfo billingExecInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                billingExecInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_BillingExec_Parameters(billingExecInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_BILLING_EXEC.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_BillingExec_Parameters(BillingExecInfo billingExecInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.EXECUTE_DATE, SqlDbType.DateTime, 8), new SqlParameter(this.STATUS, SqlDbType.VarChar, 20), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            if (!string.IsNullOrEmpty(billingExecInfo.WhID))
            {
                paramArray[0].Value = billingExecInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = billingExecInfo.ExecuteDate;
            paramArray[2].Value = billingExecInfo.Status;
            paramArray[3].Value = billingExecInfo.CreatedBy;
            paramArray[4].Value = billingExecInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateBillingExec(BillingExecInfo billingExecInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                billingExecInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_BillingExec_Parameters(billingExecInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_BILLING_EXEC.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateBillingExec(DataBase dataBase, DbTransaction tran, BillingExecInfo billingExecInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                billingExecInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_BillingExec_Parameters(billingExecInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_BILLING_EXEC.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

